package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.city_home_app.Activity.UserInfoActivity;
import com.example.city_home_app.Bean.Down_NewsReply;
import com.example.city_home_app.R;
import com.example.city_home_app.UI.MyCircleImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_NewsReply extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Down_NewsReply> list;
    private FragmentManager fragmentManager;

    public Adapter_NewsReply(Context context, FragmentManager fragmentManager) {
        this.context = context;
        this.fragmentManager = fragmentManager;
    }

    public void setList(List<Down_NewsReply> list) {
        this.list = list;
    }

    public void clearList() {
        if (list.size() != 0) {
            list.clear();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_news_reply, parent, false);
        Adapter_NewsReply.ViewHolder viewHolder = new Adapter_NewsReply.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Down_NewsReply bean = list.get(position);
        if(bean == null){
            return;
        }
        final Adapter_NewsReply.ViewHolder viewHolder = (Adapter_NewsReply.ViewHolder) holder;
        if(bean.getReply_status()==2 || bean.getReply_status() == 3){
            viewHolder.tvRecitemNewreplyContent.setText("该条回复已被删除");
            viewHolder.tvRecitemNewreplyContent.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            viewHolder.tvRecitemNewreplyContent.getPaint().setAntiAlias(true);
        }else{
            viewHolder.tvRecitemNewreplyContent.setText(bean.getReply_content());
            viewHolder.tvRecitemNewreplyContent.getPaint().setFlags(0);
        }
        viewHolder.tvRecitemNewreplyname.setText(bean.getUser_name());
        viewHolder.tvRecitemNewreplyTime.setText(bean.getReply_time());
        Picasso.with(context)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getUser_headimage())
                .into(viewHolder.imgRecitemNewreply);

        viewHolder.imgRecitemNewreply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("id",bean.getUser_id());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_recitem_newreplyname)
        TextView tvRecitemNewreplyname;
        @BindView(R.id.img_recitem_newreply)
        MyCircleImageView imgRecitemNewreply;
        @BindView(R.id.tv_recitem_newreply_content)
        TextView tvRecitemNewreplyContent;
        @BindView(R.id.tv_recitem_newreply_time)
        TextView tvRecitemNewreplyTime;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
